package com.avocado.configuration.validator;

import com.avocado.configuration.repository.SequenceRepository;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class SequenceAddNameValidator implements ConstraintValidator<SequenceAddNameValidation, String> {


    private final SequenceRepository sequenceRepository;

    public SequenceAddNameValidator(SequenceRepository sequenceRepository) {
        this.sequenceRepository = sequenceRepository;
    }

    @Override
    public void initialize(SequenceAddNameValidation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {


        try {

            long count=sequenceRepository.getCountByName(value);

            return count<1L;

        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage());
        }
        return false;
    }
}
